package com.vote.action.question;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.vote.action.BaseAction;
import com.vote.entity.AnswerInfo;
import com.vote.entity.QuestionInfo;
import com.vote.entity.Respondent;
import com.vote.service.AnswerInfoManager;
import com.vote.service.PrizeRecordManager;
import com.vote.service.QuestionInfoManager;
import com.vote.service.QuestionOptManager;
import com.vote.service.RespondentManager;
import com.vote.util.Struts2Utils;
import com.vote.util.VoteConstants;

public class QuestionAction extends BaseAction{
	
	private static final long serialVersionUID = -529911837560742552L;
	private static Logger log = LoggerFactory.getLogger(QuestionAction.class);
	
	private static Long answerCnt=0l;
	
	private static final String PRIZE_STATUS_TRUE="prized";
	private static final String PRIZE_STATUS_FALSE="noprize";
	
	@Autowired
	private QuestionInfoManager questionInfoManager;	
	@Autowired
	private QuestionOptManager questionOptManager;
	@Autowired
	private RespondentManager respondentManager;	
	@Autowired
	private AnswerInfoManager answerInfoManager;
	@Autowired
	private PrizeRecordManager prizeRecordManager;
		
	private List<QuestionInfo> questionLst=null;
	//验证码
	private String kaptcha="";	
	private String message;	
	private String questionPart;//A或B	
	private String prizedInfo="";//没有抽过奖
	
	private String retStr="0";
	
	//问题信息
	public String questionInfo(){
		
		if(answerCnt++%2==0){
			questionPart=VoteConstants.QUESTION_PART_A;
		}else{
			questionPart=VoteConstants.QUESTION_PART_B;
		}
		//问题信息
		questionLst=questionInfoManager.listQuestionInfo(questionPart);		
		//用户信息
		String ip=this.getRealIp();//ip
		String cookieVal=this.readCookie(VoteConstants.COOKIE_USER_KEY);
		if(StringUtils.isEmpty(cookieVal)){			
			cookieVal=String.valueOf(System.currentTimeMillis());//cookie
			this.saveCooke(VoteConstants.COOKIE_USER_KEY, cookieVal);
		}		
		Date now=new Date();
		log.info("ip:"+ip+" cookie:"+cookieVal);		
		Respondent resp=new Respondent();
		resp.setIp(ip);
		resp.setCookie(cookieVal);
		resp.setBackUrl(this.getReferer());//访问来源	
		resp.setCreateTime(now);
		resp.setUpdateTime(now);		
		respondentManager.saveResp(resp);		
		return SUCCESS;
	}
	
	//答题
	public String answerQuestion(){
		message=null;
		//判断有没有抽过奖
		String ip=this.getRealIp();//ip
		String cookieVal=this.readCookie(VoteConstants.COOKIE_USER_KEY);
		Boolean prizeFlag=prizeRecordManager.isHavePrizeRecords(ip, cookieVal);
		if(!prizeFlag){
			prizedInfo=PRIZE_STATUS_FALSE;
		}else{
			prizedInfo=PRIZE_STATUS_TRUE;
		}		
		questionLst=questionInfoManager.listQuestionInfo(questionPart);		
		//验证码
		/*
		String kaptchaExpected = (String)ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if(!kaptcha.equals(kaptchaExpected)){
			message="验证码不正确！";
			return ERROR;
		}
		*/
		
		AnswerInfo an=null;
		Date now=new Date();
		List<AnswerInfo> answerLst=new ArrayList<AnswerInfo>(); 			
		if(questionLst!=null && questionLst.size()>0){
			for(QuestionInfo qust:questionLst){
				String answerStr=request.getParameter("question_"+qust.getId());
				if(StringUtils.isNotEmpty(answerStr)){
					an=new AnswerInfo();
					an.setCookie(cookieVal);
					an.setIp(ip);
					an.setPart(questionPart);
					an.setQuestionInfo(qust);
					an.setQuestionOpt(questionOptManager.getQuestionOptByPk(Long.parseLong(answerStr)));
					an.setRespondent(respondentManager.findRespondentByIpOrCookie(ip, cookieVal));
					an.setCreateTime(now);			
					an.setUpdateTime(new Date());
					answerLst.add(an);
				}else{
					message="请将问卷回答完整！";
					return ERROR;
				}
			}
		}		
		if(answerLst==null || answerLst.isEmpty()){
			message="请将问卷回答完整！";
			return ERROR;
		}		
		Boolean flag=answerInfoManager.batchSave(answerLst);
		if(!flag){
			message="请将问卷回答完整！";
			return ERROR;
		}
		return SUCCESS;
	}
	
	//得到验证码
	public String codeInfo(){	
		log.info("=================================================="+kaptcha);
		log.info("=================================================="+kaptcha);
		log.info("=================================================="+kaptcha);
		String kaptchaExpected = (String)ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);//
		if(kaptcha.equals(kaptchaExpected)){			
			retStr="1";
		}
		Struts2Utils.renderText(retStr);
		return null;
	}
	
	public QuestionInfoManager getQuestionInfoManager() {
		return questionInfoManager;
	}
	public void setQuestionInfoManager(QuestionInfoManager questionInfoManager) {
		this.questionInfoManager = questionInfoManager;
	}
	
	public QuestionOptManager getQuestionOptManager() {
		return questionOptManager;
	}

	public void setQuestionOptManager(QuestionOptManager questionOptManager) {
		this.questionOptManager = questionOptManager;
	}

	public String getKaptcha() {
		return kaptcha;
	}
	public void setKaptcha(String kaptcha) {
		this.kaptcha = kaptcha;
	}
	public List<QuestionInfo> getQuestionLst() {
		return questionLst;
	}
	public void setQuestionLst(List<QuestionInfo> questionLst) {
		this.questionLst = questionLst;
	}
	public RespondentManager getRespondentManager() {
		return respondentManager;
	}
	public void setRespondentManager(RespondentManager respondentManager) {
		this.respondentManager = respondentManager;
	}
	public AnswerInfoManager getAnswerInfoManager() {
		return answerInfoManager;
	}
	public void setAnswerInfoManager(AnswerInfoManager answerInfoManager) {
		this.answerInfoManager = answerInfoManager;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

	public String getQuestionPart() {
		return questionPart;
	}

	public void setQuestionPart(String questionPart) {
		this.questionPart = questionPart;
	}

	public static Long getAnswerCnt() {
		return answerCnt;
	}

	public static void setAnswerCnt(Long answerCnt) {
		QuestionAction.answerCnt = answerCnt;
	}

	public PrizeRecordManager getPrizeRecordManager() {
		return prizeRecordManager;
	}

	public void setPrizeRecordManager(PrizeRecordManager prizeRecordManager) {
		this.prizeRecordManager = prizeRecordManager;
	}

	public String getPrizedInfo() {
		return prizedInfo;
	}

	public void setPrizedInfo(String prizedInfo) {
		this.prizedInfo = prizedInfo;
	}

	public String getRetStr() {
		return retStr;
	}

	public void setRetStr(String retStr) {
		this.retStr = retStr;
	}

	
}
